/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * This file is part of JMail                                              *
 * Copyright (C) 2002-2003 Yvan Norsa <norsay@wanadoo.fr>                  *
 *                                                                         *
 * JMail is free software; you can redistribute it and/or modify           *
 * it under the terms of the GNU General Public License as published by    *
 * the Free Software Foundation; either version 2 of the License, or       *
 * any later version.                                                      *
 *                                                                         *
 * JMail is distributed in the hope that it will be useful,                *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 * GNU General Public License for more details.                            *
 *                                                                         *
 * You should have received a copy of the GNU General Public License along *
 * with JMail; if not, write to the Free Software Foundation, Inc.,        *
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                 *
 *                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

/** Dialog allowing to manage a directory stored in the profile file
 * TODO : use a layout
 */
final class ManageAddresses extends JDialog
{
    /** User profile */
    private Profile profile;

    private JPanel panel;

    private JButton newEntryButton;
    private JButton editEntryButton;
    private JButton removeEntryButton;

    private SortableTable addressesTable;

    private String data[][];
    private String columnsNames[];

    private String indexes[];

    private JScrollPane scrollPane;

    private JButton closeButton;

    private ManageAddressesListener listener;

    /** Language resource */
    private ResourceBundle msgBundle;

    private boolean hasChanged;

    /** Constructor
     *  @param profile user profile
     *  @param msgBundle language resource
     */
    protected ManageAddresses(Profile profile, ResourceBundle msgBundle)
    {
	super((JFrame)null, msgBundle.getString("ManageAddresses.frameTitle"), true);

	this.profile = profile;
	this.msgBundle = msgBundle;

	listener = new ManageAddressesListener();

	hasChanged = false;

	panel = new JPanel();

	newEntryButton = new JButton(msgBundle.getString("ManageAddresses.newEntryLabel"));
	newEntryButton.addActionListener(listener);
	panel.add(newEntryButton);

	editEntryButton = new JButton(msgBundle.getString("ManageAddresses.editEntryLabel"));
	editEntryButton.addActionListener(listener);
	editEntryButton.setEnabled(false);
	panel.add(editEntryButton);

	removeEntryButton = new JButton(msgBundle.getString("ManageAddresses.removeEntryLabel"));
	removeEntryButton.addActionListener(listener);
	removeEntryButton.setEnabled(false);
	panel.add(removeEntryButton);

	columnsNames = new String[2];
	columnsNames[0] = msgBundle.getString("ManageAddresses.nameLabel");
	columnsNames[1] = msgBundle.getString("ManageAddresses.mailLabel");

	addressesTable = new SortableTable();
	addressesTable.setData(new Object[0][0], columnsNames);
	addressesTable.addListSelectionListener(listener);

	update();

	scrollPane = new JScrollPane(addressesTable);
	panel.add(scrollPane);

	closeButton = new JButton(msgBundle.getString("ManageAddresses.closeLabel"));
	closeButton.addActionListener(listener);
	panel.add(closeButton);

	setContentPane(panel);

	setSize(640, 480);
	setVisible(true);
    }

    /** Loads the adresses stored in the profile file */
    protected void update()
    {
	//Entry[] addresses = profile.getAddressesArray();       
	Entry[] addresses = profile.getAllAddressesArray();

	indexes = new String[addresses.length];
	data = new String[addresses.length][2];

	int i = 0;

	for(i = 0; i < addresses.length; i++)
	{
	    String row[] = new String[2];
	    indexes[i] = addresses[i].getEmail();

	    row[0] = addresses[i].getName();
	    row[1] = addresses[i].getEmail();
	    data[i] = row;
	}

	addressesTable.setIndexes(indexes);
	addressesTable.setData(data, columnsNames);
	addressesTable.addMouseListenerToHeaderInTable();
    }

    /** Tells wether changes were made or not
     *  @return boolean saying if the profile must be rewritten or not
     */
    protected final boolean hasChanged()
    {
	return(hasChanged);
    }

    protected void setButtonsEnabled(boolean b)
    {
	editEntryButton.setEnabled(b);
	removeEntryButton.setEnabled(b);
    }

    /** Listener for this class */ 
    private final class ManageAddressesListener implements ActionListener, ListSelectionListener
    {
	/** This method is invoked when an event is triggered
	 *  @param e event
	 */
	public final void actionPerformed(ActionEvent e)
	{
	    JButton b = (JButton)e.getSource();

	    if(b == newEntryButton)
	    {
		AddAddress addWindow = new AddAddress(profile, msgBundle);
		
		if(addWindow.wasAdded())
		{
		    update();
		    hasChanged = true;
		}

		addWindow.dispose();
	    }

	    else if(b == editEntryButton)
	    {
		Vector addresses = profile.getAddresses();
		String selectedMail = (String)addressesTable.getSelectedRowIndex();

		int size = addresses.size();

		Entry entry;

		for(int i = 0; i < size; i++)
		{
		    entry = (Entry)addresses.get(i);

		    if(entry.getEmail().compareTo(selectedMail) == 0)
		    {
			AddAddress editWindow = new AddAddress(profile, entry.getName(), entry.getEmail(), msgBundle);

			if(editWindow.wasAdded())
			{
			    addresses.remove(entry);
			    update();
			    hasChanged = true;
			}

			editWindow.dispose();
			break;
		    }
		}
	    }

	    else if(b == removeEntryButton)
	    {
		Vector addresses = profile.getAddresses();
		String selectedMail = (String)addressesTable.getSelectedRowIndex();

		int size = addresses.size();

		Entry entry;

		for(int i = 0; i < size; i++)
		{
		    entry = (Entry)addresses.get(i);

		    if(entry.getEmail().compareTo(selectedMail) == 0)
		    {
			addresses.remove(entry);
			break;
		    }
		}

		profile.setAddresses(addresses);
		hasChanged = true;
		update();
	    }

	    else if(b == closeButton)
		hide();
	}

	public void valueChanged(ListSelectionEvent e)
	{
	    if(e.getValueIsAdjusting())
		return;

	    ListSelectionModel lsm = (ListSelectionModel)e.getSource();

	    if(lsm.isSelectionEmpty())
		setButtonsEnabled(false);
	    
	    else
		setButtonsEnabled(true);
	}
    }
}
